<?php
if(!class_exists("GeSHi"))
	include "geshi/geshi/geshi.php";
	
function ParsageGeSHi($matches)
{
 // Inclure la librairie GeSHi
//BBcode pour afficher des codes pour coloriser
//$texte = preg_replace_callback('#(.+)#isU', 'ParsageGeSHi', $texte);
# Liste des langages

/*$ListeType = array(
'html' => 'HTML', 
'javascript' => 'JavaScript', 
'php' => 'php', 
'sql' => 'sql',
'css' => 'css',
'js'	=>	'javascript',
'javascript'=>	'javascript',
'actionscript' => 'ActionScript'
);*/

$ListeType = array( "actionscript" => "actionscript",
    "ada" => "ada",
    "apache" => "apache",
    "applescript" => "applescript",
    "asm" => "asm",
    "asp" => "asp",
    "autoit" => "autoit",
    "bash" => "bash",
    "blitzbasic" => "blitzbasic",
    "c" => "c",
    "c_mac" => "c_mac",
    "csharp" => "csharp",
    "cpp" => "cpp",
    "caddcl" => "caddcl",
    "cadlisp" => "cadlisp",
    "cfdg" => "cfdg",
    "cfm" => "cfm",
    "css" => "css",
    "delphi" => "delphi",
    "div" => "div",
    "dos" => "dos",
    "eiffel" => "eiffel",
    "fortran" => "fortran",
    "freebasic" => "freebasic",
    "gml" => "gml",
    "html4strict" => "html4strict",
	"html"	=>	"html4strict",
    "inno" => "inno",
    "java" => "java",
    "java5" => "java5",
    "javascript" => "javascript",
    "lisp" => "lisp",
    "lua" => "lua",
    "mpasm" => "mpasm",
    "mysql" => "mysql",
    "nsis" => "nsis",
    "objc" => "objc",
    "ocaml" => "ocaml",
    "oobas" => "oobas",
    "oracle8" => "oracle8",
    "pascal" => "pascal",
    "Perl" => "Perl",
    "php" => "php",
    "python" => "python",
    "qbasic" => "qbasic",
    "robots" => "robots",
    "ruby" => "ruby",
    "sas" => "sas",
    "scheme" => "scheme",
    "sdlbasic" => "sdlbasic",
    "smarty" => "smarty",
    "sql" => "sql",
    "tsql" => "tsql",
    "vbnet" => "vbnet",
    "vb" => "vb",
    "visualfoxpro" => "visualfoxpro",
    "xml" => "xml");
	
	$codeName = array("actionscript" => "Actionscript",
    "ada" => "ADA",
    "apache" => "Apache Log",
    "applescript" => "AppleScript",
    "asm" => "ASM",
    "asp" => "ASP",
    "autoit" => "AutoIT",
    "bash" => "Bash",
    "blitzbasic" => "BlitzBasic",
    "c" => "C",
    "c_mac" => "C for Macs",
    "csharp" => "C#",
    "cpp" => "C++",
    "caddcl" => "CAD DCL",
    "cadlisp" => "CadLisp",
    "cfdg" => "CFDG",
    "cfm" => "ColdFusion",
    "css" => "CSS",
    "delphi" => "Delphi",
    "div" => "DIV",
    "dos" => "DOS",
    "eiffel" => "Eiffel",
    "fortran" => "Fortran",
    "freebasic" => "FreeBasic",
    "gml" => "GML",
    "html4strict" => "HTML",
    "inno" => "Inno",
    "java" => "Java",
    "java5" => "Java 5",
    "javascript" => "Javascript",
    "lisp" => "Lisp",
    "lua" => "Lua",
    "mpasm" => "Microprocessor ASM",
    "mysql" => "MySQL",
    "nsis" => "NSIS",
    "objc" => "Objective C",
    "ocaml" => "OCaml",
    "oobas" => "OpenOffice BASIC",
    "oracle8" => "Oracle 8 SQL",
    "pascal" => "Pascal",
    "Perl" => "Perl",
    "php" => "PHP",
    "python" => "Python",
    "qbasic" => "Q(uick)BASIC",
    "robots" => "robots.txt",
    "ruby" => "Ruby",
    "sas" => "SAS",
    "scheme" => "Scheme",
    "sdlbasic" => "SDLBasic",
    "smarty" => "Smarty",
    "sql" => "SQL",
    "tsql" => "T-SQL",
    "vbnet" => "VB.NET",
    "vb" => "Visual BASIC",
    "visualfoxpro" => "Visual Fox Pro",
    "XML" => "xml");
	
$matches[3] = str_replace(' ','',$matches[3]);
if (!array_key_exists($matches[3], $ListeType)){

	$Type = 'AUTRE';
	$TypeCode = $matches[3];

}else{

	$TypeCode = $codeName[$matches[3]];
	$Type = $ListeType[$matches[3]];
	if($Type == 'HTML'){
		$Type = 'html4strict';
		$TypeCode = 'HTML';
	}
	
}

# Coloration
$text = htmlspecialchars_decode($matches[4]);
$geshi = new GeSHi($text, $Type);
$geshi->set_line_style('color:#D3FE89;');
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 2);  
$geshi->start_line_numbers_at(1);  
$geshi->set_line_style('background: #D3FE85;', 'background: #EAF4C6;');
//On change les couleur des code geshi
$geshi->set_keyword_group_style(1, 'color: #00CC00');
$geshi->set_keyword_group_style(2, 'color: #FF0000');
$geshi->set_keyword_group_style(3, 'color: #FFCC66');
$geshi->set_symbols_style(1, 'color: #0099CC');
$geshi->set_highlight_lines_extra_style('background-color:#FFD9D9');
$geshi->set_header_type(GESHI_HEADER_PRE);

$CodeParse = $geshi->parse_code();

$CodeParse = str_replace(chr(10), '', $CodeParse); // Pour supprimer les retour à la ligne en trop
// Pour supprimer les retour à la ligne en trop
//DEBUT'.$matches[1].'MILIEU'.$matches[2].'FIN

	if($TypeCode == "css")
		$TypeCode = "<span style=\"color:#FF002C\">" . $TypeCode . "</span>";
		
return 'Code : ' . $TypeCode . '<div class="geshi_contenu"> ' . str_replace('  ','',$CodeParse) . '</div>';

}

function CallVideo($matches){
		return getVideoString($matches[1]);	
}

function CallTableau($matches)
{
	$in = array('&lt;cell&gt;','&lt;/cell&gt;',
				'&lt;titre&gt;','&lt;/titre&gt;',
				'&lt;ligne&gt;','&lt;/ligne&gt;',
				'&lt;nom&gt;','&lt;/nom&gt;',
				'&lt;td&gt;','&lt;/td&gt;',
				'&lt;caption&gt;','&lt;/caption&gt;',
				'&lt;tr&gt;','&lt;/tr&gt;',
				'&lt;th&gt;','&lt;/th&gt;');
	$out = array('<td>','</td>',
				'<caption>','</caption>',
				'<tr>','</tr>',
				'<th>','</th>',
				'<td>','</td>',
				'<caption>','</caption>',
				'<tr>','</tr>',
				'<th>','</th>');
				
				
				
				
				
   $content = '';
   if (!empty($matches[1])) {
      //$content = preg_replace('/<titre>(.+)</titre>/', '<caption>$1</caption>', $matches[1]);
      $content = str_replace($in, $out, $matches[1]);

      $content = '<table>'.$content.'</table>';
   }
   return $content;
}

function code($texte)
{
	$lien = DIR_DESIGN.'/'.getDesignName().'/images';
		if(!is_dir($lien))
			$lien = "../" . $lien;
		if(!is_dir($lien))
			$lien = "../../".$lien;
		else
			$lien = $lien;

//$texte = str_replace('<code type=[[:space:]]','<code type=',$texte);

if(!isEncoded_htmlspecialchars($texte))
	$texte = htmlspecialchars($texte);
	
//$texte .= preg_replace_callback('#<code type=(.+)>(.+)</code>#isU', 'ParsageGeSHi', $textGeshi);
//$texte = callback('#<code type( *)=( *)(.+)>(.+)</code>#isU', 'ParsageGeSHi', $texte);
$texte = preg_replace_callback('#'.htmlspecialchars('<code type( *)=( *)(.+)>(.+)</code>').'#isU', 'ParsageGeSHi', $texte);
$texte = preg_replace_callback("#" . htmlspecialchars("<video>(.+)</video>") . "#isU",'CallVideo',$texte);
$texte = preg_replace_callback("#" . htmlspecialchars("<tableau>(.+)</tableau>") . "#isU",'CallTableau',$texte);

$texte = preg_replace("#" . htmlspecialchars("<information>(.+)</information>") . "#isU",'<img src="'.$lien.'/formulaires/information.png" boder="0">&nbsp;&nbsp; <span style="color:rgb(21,111,48);">$1</span>',$texte);

$texte = preg_replace("#" . htmlspecialchars("<erreur>(.+)</erreur>") . "#isU",'<img src="'.$lien.'/formulaires/erreur.png" boder="0">&nbsp;&nbsp; <span style="color:#F00;">$1</span>',$texte);

$texte = preg_replace("#" . htmlspecialchars("<attention>(.+)</attention>") . "#isU",'<img src="'.$lien.'/formulaires/attention.png" boder="0">&nbsp;&nbsp; <span style="color:rgb(215,90,0);">$1</span>',$texte);

$texte = preg_replace("#" . htmlspecialchars("<question>(.+)</question>") . "#isU",'<img src="'.$lien.'/formulaires/question.png" boder="0">&nbsp;&nbsp; <span style="color:#11E4F7;">$1</span>',$texte);

$texte = preg_replace("#" . htmlspecialchars("<gras>(.+)</gras>") . "#isU",'<strong>$1</strong>',$texte);
$texte = preg_replace("#" . htmlspecialchars("<italique>(.+)</italique>") . "#isU",'<i>$1</i>',$texte);
$texte = preg_replace("#" . htmlspecialchars("<souligner>(.+)</souligner>") . "#isU",'<u>$1</u>',$texte);
$texte = preg_replace("#" . htmlspecialchars("<barrer>(.+)</barrer>") . "#isU",'<s>$1</s>',$texte);

$texte = preg_replace("#" . htmlspecialchars("<position valeur( *)=( *)([a-z-A-Z]+)( *)>(.+)</position>") . "#isU",'<span class="$3">$5</span>',$texte);

$texte = preg_replace("#" . htmlspecialchars("<flottant valeur( *)=( *)([a-z-A-Z]+)>(.+)</flottant>") . "#isU",'<span class="$3">$4</span>',$texte);

$texte = preg_replace("#" . htmlspecialchars("<taille valeur( *)=( *)([a-z-A-Z]+)>(.+)</taille>") . "#isU",'<span class="$3">$4</span>',$texte);

$texte = preg_replace("#" . htmlspecialchars("<couleur nom( *)=( *)([a-z-A-Z]+)>(.+)</couleur>") . "#isU",'<span class="$3">$4</span>',$texte);

$texte = preg_replace("#" . htmlspecialchars("<police valeur( *)=( *)([a-z-A-Z]+)>(.+)</police>") . "#isU",'<span class="$3">$4</span>',$texte);

$texte = preg_replace("#" . htmlspecialchars("<titre1>(.+)</titre1>") . "#isU",'<span class="titre_1">$4</span>',$texte);
$texte = preg_replace("#" . htmlspecialchars("<titre2>(.+)</titre2>") . "#isU",'<span class="titre_2">$4</span>',$texte);

$texte = preg_replace("#" . htmlspecialchars("<image>(.+)</image>") . "#isU",'<img src="$1" boreder="0" alt="Image utilisateur" title="Image utilisateur" style="max-width:750px; max-height:280px;" />',$texte);

$texte = preg_replace("#" . htmlspecialchars("<image lien( *)=( *)(.+)>(.+)</police>") . "#isU",'<img src="$3" boreder="0" alt="$4" title="$4" style="max-width:750px; max-height:280px;"/>',$texte);

//citation
$texte = preg_replace("#" . htmlspecialchars("<citation>(.+)</citation>") . "#isU",'<div class=citation>Citation</div>',$texte);

$texte = preg_replace("#" . htmlspecialchars("<citation( *)=( *)(.+)>(.+)</citation>") . "#isU",'Citation $3 <div class=citation>$4</div>',$texte);

$texte = preg_replace("#" . htmlspecialchars("<mail>([a-z-A-Z_/\-]+)([a-z-A-Z_/\-\.]+)\.([a-z]{2,4})</mail>") . "#isU",'<a href="mailto:$1$2.$3" target="_blank">$1$2.$3$4</a>',$texte);

$texte = preg_replace("#" . htmlspecialchars("<mail lien( *)=( *)([a-z-A-Z_/\-]+)([a-z-A-Z_/\-\.]+)\.([a-z]{2,4})>(.+)</mail>") . "#isU",'<a href="mailto:$3$4.$5" target="_blank">$6</a>',$texte);

$texte = preg_replace("#" . htmlspecialchars("<lien>((https?://)((([^\.])([www]{0,1})\.?)[a-zA-Z0-9_\.]+)\.([a-z]{2,})((.+)?))</lien>") . "#isU",'<a href="$1" target="_blank">$1</a>',$texte);

$texte = preg_replace("#" . htmlspecialchars("<lien( *)=( *)((https?://)((([^\.])([www]{0,1})\.?)[a-zA-Z0-9_\.]+)\.([a-z]{2,}))>(.+)</lien>") . "#isU",'<a href="$3" target="_blank">$10$11</a>',$texte);

$texte = preg_replace("#https?://[a-z0-9._/-]+([a-z0-9._/-\?]+)?#i",'<a href="$0" target="_blank">$0</a>',$texte);

$texte = preg_replace("#" . htmlspecialchars("<lien( *)=( *)(.+)>(.+)</lien>") . "#isU",'<a href="'.$_SERVER['REQUEST_URI'].'$3" target="_blank">$4</a>',$texte);



	
$texte = preg_replace("#" . htmlspecialchars("<separator>") . "#isU",'<hr />',$texte);

if(substr($texte,0,8)==htmlspecialchars("<ancre>#"))
	$texte = str_replace("#","",substr($texte,8));
	
$texte = preg_replace("#" . htmlspecialchars("<ancre>(.+)</ancre>") . "#isU",'<a name="$1"></a>',$texte);
$texte = preg_replace("#" . htmlspecialchars("<chapitre numero=\"(.+)\">(.+)</chapitre>") . "#isU",'<a name="chapitre_$1"></a>$2<hr />',$texte);

$texte = preg_replace("#" . htmlspecialchars("<urlchapitre numero=\"(.+)\">(.+)</urlchapitre>") . "#isU",'<a href="'.$_SERVER['REQUEST_URI'].'#chapitre_$1">$2</a>',$texte);

$texte = preg_replace("#" . htmlspecialchars("<secret>(.+)</secret>") . "#isU",'Secret : <a onclick="updown_secret();" style="cursor:pointer; ">Cliquez ici pour montrer/cacher</a><fieldset><div id="D_Secret" style="display:none;">$1</div></fieldset>',$texte);

	
	
		//$texte = preg_replace("#" . htmlspecialchars("<(.+)>") . "#isU","&lt;$1&gt;",$texte);
		//$texte = str_replace("\n","",substr($texte,strpos("<liste>",$texte),strpos("</liste>",$texte)));
		$texte = preg_replace("#" . htmlspecialchars("<liste>(.+)</liste>") . "#isU","<ul class=\"carre\">$1</ul>",$texte);
		$texte = preg_replace("#" . htmlspecialchars("<liste nom( *)=( *)(.+)>(.+)</liste>") . "#isU","<fieldset><legend>$3</legend><ul class=\"carre\">$4</ul></fieldset>",$texte);
		$texte = preg_replace("#" . htmlspecialchars("<puce>(.+)</puce>") . "#isU","<li>$1</li>",$texte);
		$texte = preg_replace("#" . htmlspecialchars("<puce>(.+)") . "#isU","<span class=\"info\">$1</span>",$texte);

		//$texte = preg_replace("#" . htmlspecialchars("<(.+)>") . "#isU","&lt;$1&gt;",$texte);
		//$texte = str_replace("\n","",substr($texte,strpos("<liste>",$texte),strpos("</liste>",$texte)));
		$texte = preg_replace("#" . htmlspecialchars("<liste>(.+)</liste>") . "#isU","<ol class=\"carre\">$1</ol>",$texte);
		$texte = preg_replace("#" . htmlspecialchars("<liste nom( *)=( *)(.+)>(.+)</liste>") . "#isU","<fieldset><legend>$3</legend><ol class=\"carre\">$4</ol></fieldset>",$texte);
		$texte = preg_replace("#" . htmlspecialchars("<numero>(.+)</numero>") . "#isU","<li>$1</li>",$texte);
		$texte = preg_replace("#" . htmlspecialchars("<numero>(.+)") . "#isU","<span class=\"info\">$1</span>",$texte);
		
		//$texte = preg_replace("#^(https?://)([a-zAZ-_-.-\-]+.([a-zAZ){2,4}/(.+)$#isU","<a href=\"$1$2\">$1$2</a>",$texte);



// Smileys Principaux
// Smileys Clin

$texte = str_replace(':clin:', '<img src="' . $lien . '/smileys/clin.png" alt="clin" title="clin" />', $texte);

// Smileys Blink

$texte = str_replace(':blink:', '<img src="' . $lien . '/smileys/blink.gif" alt="blink" title="blink" />', $texte);

// Smileys Heureux

$texte = str_replace(':heureux:', '<img src="' . $lien . '/smileys/heureux.png" alt="heureux" title="heureux" />', $texte);

// Smileys Hihi

$texte = str_replace(':hihi:', '<img src="' . $lien . '/smileys/hihi.png" alt="hihi" title="hihi" />', $texte);

// Smileys Huh

$texte = str_replace(':huh:', '<img src="' . $lien . '/smileys/huh.png" alt="huh" title="huh" />', $texte);

// Smileys Langue

$texte = str_replace(':langue:', '<img src="' . $lien . '/smileys/langue.png" alt="langue" title="langue" />', $texte);

// Smileys Méchant

$texte = str_replace(':mechant:', '<img src="' . $lien . '/smileys/mechant.png" alt="mechant" title="mechant" />', $texte);

// Smileys Rire

$texte = str_replace(':rire:', '<img src="' . $lien . '/smileys/rire.gif" alt="rire" title="rire" />', $texte);
// Smileys Triste

$texte = str_replace(':triste:', '<img src="' . $lien . '/smileys/triste.png" alt="triste" title="triste" />', $texte);
// Smileys Siffle

$texte = str_replace(':siffle:', '<img src="' . $lien . '/smileys/siffle.png" alt="siffle" title="siffle" />', $texte);
// Smileys Smile

$texte = str_replace(':smile:', '<img src="' . $lien . '/smileys/smile.png" alt="smile" title="smile" />', $texte);
// Smileys Unsure

$texte = str_replace(':unsure:', '<img src="' . $lien . '/smileys/unsure.gif" alt="unsure" title="unsure" />', $texte);

// Smileys Du Pop Up
// Smileys Ange

$texte = str_replace(':ange:', '<img src="' . $lien . '/smileys/ange.png" alt="ange" title="ange" />', $texte);

// Smileys Angry

$texte = str_replace(':angry:', '<img src="' . $lien . '/smileys/angry.gif" alt="angry" title="angry" />', $texte);

// Smileys Diable

$texte = str_replace(':diable:', '<img src="' . $lien . '/smileys/diable.png" alt="diable" title="diable" />', $texte);

// Smileys Magicien

$texte = str_replace(':magicien:', '<img src="' . $lien . '/smileys/magicien.png" alt="magicien" title="magicien" />', $texte);

// Smileys Ninja

$texte = str_replace(':ninja:', '<img src="' . $lien . '/smileys/ninja.png" alt="ninja" title="ninja" />', $texte);

// Smileys Pinch

$texte = str_replace(':pinch:', '<img src="' . $lien . '/smileys/pinch.png" alt="pinch" title="pinch" />', $texte);

// Smileys Pirate


$texte = str_replace(':pirate:', '<img src="' . $lien . '/smileys/pirate.png" alt="pirate" title="pirate" />', $texte);
// Smileys Pleure

$texte = str_replace(':pleure:', '<img src="' . $lien . '/smileys/heureux.png" alt="pleure" title="pleure" />', $texte);
// Smileys Rouge

$texte = str_replace(':rouge:', '<img src="' . $lien . '/smileys/rouge.png" alt="rouge" title="rouge" />', $texte);
// Smileys Soleil

$texte = str_replace(':soleil:', '<img src="' . $lien . '/smileys/soleil.png" alt="soleil" title="soleil" />', $texte);
// Smileys Waw


$texte = str_replace(':waw:', '<img src="' . $lien . '/smileys/waw.png" alt="waw" title="waw" />', $texte);
// Smileys Zorro

$texte = str_replace(':zorro:', '<img src="' . $lien . '/smileys/zorro.png" alt="zorro" title="zorro" />', $texte);


// On retourne la variable texte

return $texte;
}

	
	
//Shinte pour eviter les doublons et pouvoir ainsi envoyer le contenu geshi dans la bonne fonction, sinon contenu + htmlspecialchars
	function callback($regexp,$function,$display){
		if(preg_match($regexp,$display)){
			return preg_replace_callback($regexp, $function, htmlspecialchars_decode($display));
		}else
			return  $display;
	}
	
	function isEncoded_htmlspecialchars($str,$ent_quote=null){
		$encode_base = get_html_translation_table(HTML_SPECIALCHARS,$ent_quote);
		$encodedSpecialchars = true;
		
			for($i=0;$i<strlen($str);$i++)
			{
				
				if(isset($encode_base[$str{$i}]) && $encodedSpecialchars==true)
				{
					$encodedSpecialchars = false;
				}
			}
			
			
		return $encodedSpecialchars;
		
	}
?>